package com.sealer.pattern.behavioral.iterator;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by sealer on 17/04/06.
 */
public class Set<T> implements Collection<T> {
    private static final Object PRESENT = new Object();
    private final Map<T, Object> map = new HashMap<>();

    @Override
    public Iterator<T> getIterator() {
        return new Iterator<T>() {
            int index = 0;
            T[] ts = (T[]) map.keySet().toArray();

            @Override
            public boolean hasNext() {
                return index < map.size();
            }

            @Override
            public T next() {
                return ts[index++];
            }
        };
    }

    @Override
    public boolean add(T t) {
        return map.put(t, PRESENT) == null;
    }

    @Override
    public boolean remove(T t) {
        return map.remove(t) == PRESENT;
    }

    @Override
    public int size() {
        return map.size();
    }

}
